﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using MasterCSharp.WebServices;
using System.Collections;
using System.IO;

namespace WebService
{
    /// <summary>
    /// Summary description for Update
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

    public class Update : System.Web.Services.WebService
    {
        private UpdateInfos GetUpdateInfos(string filepath, string version)
        {
            UpdateInfos ui = new UpdateInfos();
            
            //lấy vị trí xuất hiện của tên thư mục trong đường dẫn đến file cần update
            int start = filepath.IndexOf(version, 0, filepath.Length - 1);
            //lấy tên file cần update
            string virtualpath = filepath.Substring(start + version.Length, (filepath.Length) - (start + version.Length));

            ui.filepath = virtualpath;
            ui.time = System.IO.File.GetLastWriteTimeUtc(filepath);

            return ui;
        }

        private UpdateInfos GetUpdateInfosAndBinary(string filepath, string version)
        {
            UpdateInfos ui = new UpdateInfos();
            System.IO.FileStream fs = System.IO.File.OpenRead(filepath);

            //get the relativepath
            int start = filepath.IndexOf(version, 0, filepath.Length - 1);
            string virtualpath = filepath.Substring(start + version.Length, (filepath.Length) - (start + version.Length));

            ui.filepath = virtualpath;
            ui.bin = new byte[fs.Length];
            fs.Read(ui.bin, 0, (int)fs.Length);
            fs.Close();

            ui.time = System.IO.File.GetLastWriteTimeUtc(filepath);

            return ui;
        }

        private List<UpdateInfos> GetUpdateInfosRecursive(string path, string version)
        {
            List<UpdateInfos> ups = new List<UpdateInfos>();

            string[] files = Directory.GetFiles(path);
            string[] dirs = Directory.GetDirectories(path);

            foreach (string file in files)
            {
                ups.Add(GetUpdateInfos(file, version));
            }

            foreach (string dir in dirs)
            {
                if ((File.GetAttributes(dir) & FileAttributes.Hidden) != FileAttributes.Hidden)
                {
                    List<UpdateInfos> internalresult = GetUpdateInfosRecursive(dir, version);
                    foreach (UpdateInfos u in internalresult)
                    {
                        ups.Add(u);
                    }
                }
            }

            return ups;

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="version">Thư mục cần kiểm tra có file cần update không</param>
        /// <returns></returns>
        [WebMethod]
        //[CompressionExtension]        
        public List<UpdateInfos> GetAllUpdateInfosCompressed(string version)
        {
            try
            {
                string path = Server.MapPath("Updates/" + version + "/");
                
                return GetUpdateInfosRecursive(path, version);
            }
            catch
            {
                return null;
            }

        }


        [WebMethod]
        //[CompressionExtension]
        public UpdateInfos GetFileCompressed(string version, string fpath)
        {
            try
            {
                string filepath = Server.MapPath("Updates/" + version + "/" + fpath);

                if (System.IO.File.Exists(filepath))
                {

                    return GetUpdateInfosAndBinary(filepath, version);

                }

            }
            catch
            {
                return null;
            }


            return null;


        }


        [WebMethod]
        //[CompressionExtension]
        public string UploadFileCompressed(string version, string fpath, byte[] content)
        {
            try
            {
                string filepath = Server.MapPath("Updates/" + version + "/" + fpath);

                if (System.IO.File.Exists(filepath))
                {
                    System.IO.File.Delete(filepath);
                }

                System.IO.FileStream fs = System.IO.File.Create(filepath);
                fs.Write(content, 0, content.Length);
                fs.Close();

            }
            catch (Exception ex)
            {
                return "Upload Failed : " + ex.Message + "\r\n" + ex.StackTrace;
            }


            return "Upload done !";


        }

        
    }
}
